
import { registerClass } from "../../../cfw/tools/class_decorator";
import BaseAd from "../../core/base/BaseAd";
import { ADName, SDKDir, SDKLogicState, SDKState } from "../../SDKDefine";
/**
 * https://smartprogram.baidu.com/docs/game/tutorials/adTutorial/bannerDoc/。
 */
@registerClass('SwanBannerAd')
export default class SwanBannerAd extends BaseAd {
    hide(): void {
        throw new Error("Method not implemented.");
    }
    load(): void {
        throw new Error("Method not implemented.");
    }
    show(): void {
        throw new Error("Method not implemented.");
    }


    updateSize() {
        if (!this.ad) {
            return;
        }
        let winSize = this.sdk.getSystemInfoSync();
        if (this.style.width != 0 && this.style.height != 0 && this.ad) {
            switch (this.param.dir) {
                case SDKDir.BOTTOM_LEFT:
                    this.ad.style.top = (winSize.windowHeight - this.style.height);
                    this.ad.style.left = 0;
                    break;
                case SDKDir.BOTTOM_MID:
                    this.ad.style.top = (winSize.windowHeight - this.style.height);
                    this.ad.style.left = (winSize.windowWidth - this.style.width) / 2;
                    break;
                case SDKDir.RIGHT_MID:
                    this.ad.style.top = (winSize.windowHeight - this.style.height) / 2;
                    this.ad.style.left = winSize.windowWidth - this.style.width;
                    break;
                case SDKDir.MID:
                    this.ad.style.top = (winSize.windowHeight - this.style.height) / 2;
                    this.ad.style.left = (winSize.windowWidth - this.style.width) / 2;
                    break;
                case SDKDir.TOP_MID:
                    this.ad.style.top = 0;
                    this.ad.style.left = (winSize.windowWidth - this.style.width) / 2;
                    break;
                case SDKDir.XY:
                    this.ad.style.top = this.style.top// this.ry * winSize.windowHeight;
                    this.ad.style.left = this.style.left// this.rx * winSize.windowWidth;
                    break;
            }

        }
    }

    getStyle(): any {
        let winSize = this.sdk.getSystemInfoSync();
        let x = 0
        let y = 0
        switch (this.param.dir) {
            case SDKDir.BOTTOM_MID:
                x = (winSize.windowWidth - this.style.width) / 2
                y = winSize.windowHeight - this.style.height;
                break;
            case SDKDir.UP_MID:
                x = (winSize.windowWidth - this.style.width) / 2
                y = 0
                break;
            case SDKDir.MID:
                x = (winSize.windowWidth - this.style.width) / 2
                y = (winSize.windowHeight - this.style.height) / 2;
                break;
            case SDKDir.RIGHT_MID:
                x = (winSize.windowWidth - this.style.width)
                y = (winSize.windowHeight - this.style.height) / 2;
                break;
            case SDKDir.LEFT_TOP:
                x = 0
                y = 0
                break;
            case SDKDir.BOTTOM_LEFT:
                x = 0
                y = winSize.windowHeight - this.style.height;
                break;
            case SDKDir.XY:
                x = this.style.left
                y = this.style.top;
                break;
            case SDKDir.WHITE:
                x = -this.style.width + 1
                y = -this.style.height + 1

                break;
        }
        return {
            left: x,
            top: y,
            width: this.style.width,
            height: this.style.height,
        }
    }


    create(): void {

        if (!this.ad) {
            this.ad = this.sdk.createBannerAd({
                adUnitId: this.adUnitID,
                appSid: this.system.getParamValue(ADName.appid),
                style: this.getStyle()
            })
            this.ad.onLoad(this.onLoad.bind(this))
            this.ad.onError(this.onError.bind(this))
            this.ad.onResize(this.onResize.bind(this))
        }


    }

    onResize(data: { width: number, height: number }) {
        // console.log(' banner onResize ', data)
        this.style.width = data.width;
        this.style.height = data.height;
        this.updateSize()
    }

    destroy(): void {
        console.log(' banner destroy', this.ad)
        if (this.ad) {
            this.ad.offLoad(this.getFunc(this.onLoad))
            this.ad.offError(this.getFunc(this.onError))
            this.ad.offResize(this.onResize.bind(this))
            this.ad.destroy();
            this.ad = null;
        }
    }

}
